package inl.resilienceManager.qmul.gemom;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import resilienceManager.qmul.gemom.NSitem;



/**
 * The class used for the variance table of items
 * 
 * The variable number of item is used
 * 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */

public class itemTable {
	private JTable table;
	
	private DefaultTableModel model;

	private JScrollPane scrollPane;

	private JPanel itemPane;
	
	private int numItem;
	
	
	/**
	 * constructor class 
	 * 
	 * @param itemlist: NSitem[], the array of items
	 * @param dataTable: float[][], the variance table for the 
	 * 
	 * 
	 * */
	public itemTable(NSitem[] itemlist)
	{
		
		
		float[][] dataTable= TableSaver.OtoF(TableSaver.Loading());
		
		numItem=itemlist.length;
		
		String[] columnNames= new String[numItem];

		for(int i=1; i<numItem+1; i++)
		{
			columnNames[i-1]="Item"+i;
		}
		
		
		float[][] dataF= new float[numItem][numItem];
		
		Object[][] data= new Object[numItem][numItem];
		for(int i=0; i<numItem; i++)
		{
			for(int j=0; j<numItem; j++)
				
				dataF[i][j]=0;
		}
		
		for(int i=0; i<numItem; i++)
		{
			
			for(int j=0; j<numItem; j++)
				
			{
				if(dataF[i][j]==0)
				{
					if(i==j)
					{
						dataF[i][j]=VarianceCalculation.sumVar(dataTable, itemlist[i]);
						data[i][j]=(Object)String.valueOf(dataF[i][j]);
					}
					else
					{
						dataF[i][j]=VarianceCalculation.ConVarItem(dataTable, itemlist[i], itemlist[j]);
						dataF[j][i]=dataF[i][j];
						data[i][j]=(Object)String.valueOf(dataF[i][j]);
						data[j][i]=(Object)String.valueOf(dataF[i][j]);
					}
				}
			}
		}
		
		model= new DefaultTableModel(data,columnNames);
		



		table = new JTable(model){
		      public Component prepareRenderer(TableCellRenderer renderer,int rowIndex, int vColIndex) {
		    	  Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
		            if (c instanceof JComponent) {
		                JComponent jc = (JComponent)c;
		                jc.setToolTipText(getValueAt(rowIndex, vColIndex).toString());
		            }
		            return c;
		      }
		     };


		/*table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumn column = null;
		
		for (int i = 0; i < numItem; i++) {
		    column = table.getColumnModel().getColumn(i);
		    column.setPreferredWidth(80);
		}
		*/
	}
	
	/**
	 * 
	 * @return itemPane: JPanel
	 * 
	 * **/
	public JPanel getPane()
	{
		itemPane= new JPanel();

		scrollPane= new JScrollPane(table);
		
		itemPane.setLayout( new BorderLayout() );

		itemPane.add(scrollPane,BorderLayout.CENTER);
		
		return itemPane;
	}
	
}
